var groupManager_page={};

groupManager_page.action = {
    initBody:function () {
        groupManager_page.interface.initTablePage();
        groupManager_page.action.bindEvent();
    },
    bindEvent:function () {
        $("#addBtn").click(function () {
            // groupManager_page.interface.initFormPage();
            groupManager_page.interface.openFormPage();
            return false;
        });
        $("#saveBtn").click(function () {
            groupManager_page.action.saveBtnClick();
            return false;
        });
        $("#cancelBtn").click(function () {
            groupManager_page.action.initBody();
            return false;
        });
        // 查看组织树
        $("#viewGroupTreeBtn").click(function () {
            groupManager_page.interface.initGroupTree();
            return false;
        });
    },
    saveBtnClick:function () {
        // console.log(groupManager_page.data.dtreeIns.selectVal('parentName'));
        var parentIdValue = $("input[name='parentName_select_nodeId']").val();
        var parentNameValue = $('#parentName_select_input_id').val();
        if (parentIdValue==undefined || parentIdValue==null) {
            parentIdValue = $("#parentId").val();
        }
        if (parentNameValue==undefined || parentNameValue==null) {
            parentNameValue = $("#parentName").val();
        }
        var params = {
            id:$("#id").val(),
            groupName:$("#groupName").val(),
            comment:$("#comment").val(),
            orderId:$("#orderId").val(),
            parentId:parentIdValue,
            parentName:parentNameValue
        };
        var result = groupManager_page.action.checkData(groupManager_page.data.action, params);
        if (result.code==Enums.ResultEnum().FAILURE.code) {
            AppComm.layer.warm(result.msg);
        } else {
            AppComm.ajax.post('/groupManager/ajax/saveGroup', params);
        }
    },
    // 在做action动作时校验数据data
    checkData:function (action, data) {
        var result = AppComm.data.newResult();

        if (action=='addSubmit') {
            if(data.parentId==""||data.parentId==null||data.parentId.trim()==""){
                result.code = Enums.ResultEnum().FAILURE.code;
                result.msg = "上级组织不能为空。";
                return result;
            }
            if(data.groupName.length>90){
                result.code = Enums.ResultEnum().FAILURE.code;
                result.msg = "组织名称不能超过40个汉字。";
                return result;
            }
            if(data.orderId==""||data.orderId==null||data.orderId.trim()==""){
                result.code = Enums.ResultEnum().FAILURE.code;
                result.msg = "排序号不能为空。";
                return result;
            }
        }
        return result;
    },
    deleteGroup:function (id) {
        AppComm.ajax.post('/groupManager/ajax/deleteGroup',{
            id:id
        },function (resultData) {
            groupManager_page.action.initBody();
        });
    }
};
groupManager_page.interface = {
    // 加载表格页
    initTablePage:function () {
        var html = template("groupManagerList_template");
        $(".groupManager_box").html(html);

        layui.config({
            base: '/static/comm/plugins/layui/lay/modules/',
        });
        layui.use(['treeTable','layer','code','form', 'util'],function(){
            var o = layui.$,
                form = layui.form,
                layer = layui.layer,
                treeTable = layui.treeTable;

            var onSuccess = function (resultData) {
                var	re = treeTable.render({
                    elem: '#tree-table',
                    data: resultData.data,
                    method: 'POST',
                    contentType: 'application/json',
                    icon_key: 'groupName',
                    primary_key: 'id',
                    parent_key: 'parentId',
                    is_open: true,
                    end: function(e){
                        form.render();
                    },
                    cols: [
                        {
                            key: 'groupName',
                            title: '组织名称',
                            width: '300px',
                            template: function(item){
                                if(item.level == 0){
                                    return '<span style="color:red;">'+item.groupName+'</span>';
                                }else if(item.level == 1){
                                    return '<span style="color:green;">'+item.groupName+'</span>';
                                }else if(item.level == 2){
                                    return '<span style="color:blue;">'+item.groupName+'</span>';
                                } else {
                                    return '<span style="color:blue;">'+item.groupName+'</span>';
                                }
                            }
                        },
                        {
                            key:'level',
                            title:'层级',
                            align:'left',
                            width:80
                        },
                        {
                            key:'parentName',
                            title:'上级组织',
                            align:'left',
                            width:200
                        },{
                            key:'orderId',
                            title:'排序号',
                            // edit: 'text',
                            align:'left',
                            width:100
                        },{
                            key:'comment',
                            title:'说明',
                            align:'left',
                            width:300
                        },
                        {
                            title: '操作',
                            align: 'center',
                            template: function(item){
                                var renderHtml = '';
                                renderHtml = renderHtml + '<a class="layui-btn layui-btn-primary layui-btn-xs editBtn" data-id='+item.id+'>编辑</a>';
                                renderHtml = renderHtml + '<a class="layui-btn layui-btn-primary layui-btn-xs deleteBtn" data-id='+item.id+' data-level='+item.level+'>删除</a>';
                                return renderHtml;
                            }
                        }
                    ]
                });
            };
            AppComm.ajax.execute({
                'url':AppComm.url.getBaseURL()+'/groupManager/ajax/getGroupList4TableTree',
                'data':'',
                'async':false,
                'success':onSuccess
            });
            $(".editBtn").click(function () {
                // groupManager_page.interface.initFormPage($(this).attr("data-id"));
                groupManager_page.interface.openFormPage($(this).attr("data-id"));
            });
            $(".deleteBtn").click(function () {
                var id = $(this).attr("data-id");
                var level = $(this).attr("data-level");
                AppComm.layer.confirm('确定要进行删除吗？',function () {
                    groupManager_page.action.deleteGroup(id);
                });
            });
        });
    },
    openFormPage:function(rowId){
        AppComm.layer.open('/manager/form/groupManagerForm', null, function (layero, index) {
            var iframeWin = AppComm.utils.iframeWin(layero);
            if (rowId != undefined && '' != rowId.trim()) {
                iframeWin.groupManagerForm_page.data.rowId = rowId;
            }
            iframeWin.groupManagerForm_page.action.initBody();
        });
    },
    // 初始化表单页
    initFormPage:function (dataId) {
        if (dataId==undefined || ''==dataId.trim()) {
            // 添加
            var html = template("groupManagerForm_template");
            $(".groupManager_box").html(html);

            $("#parentNameInput").css("display","block");
            $("#parentNameShow").css("display","none");

            AppComm.ajax.post("/groupManager/ajax/getGroupList4Dtree", null, function (resultData) {
                if (resultData.code==Enums.ResultEnum().SUCCESS.code) {
                    layui.config({
                        base: '/static/comm/plugins/dtree/'
                    }).use(['dtree','layer','jquery'],function(){
                        var dtree = layui.dtree, layer = layui.layer, $ = layui.jquery;
                        dtree.render({
                            elem: "#parentName",
                            width: "100%",
                            dataFormat: "list",
                            data: resultData.data,
                            selectTips: "请选择上级组织",
                            select: true //指定下拉树模式
                        });
                        groupManager_page.data.dtreeIns = dtree;
                    });
                }
            })

            groupManager_page.action.bindEvent();
            groupManager_page.data.action="addSubmit";
        } else {
            // 编辑
            layer.load();
            AppComm.ajax.post("/groupManager/ajax/getGroupById", {
                id:dataId
            }, function (resultData) {
                layer.closeAll('loading');
                var html = template("groupManagerForm_template", resultData.data);
                $(".groupManager_box").html(html);

                $("#parentNameInput").css("display","none");
                $("#parentNameShow").css("display","block");

                groupManager_page.action.bindEvent();
                groupManager_page.data.action="editSubmit";
            })
        }
    },
    //初始化查看组织树的页面
    initGroupTree: function () {
        var params = {
            // id: dataId,
        };

        var html = template("groupManagerList_ztree", {});
        var roleAuthLayer = layer.open({
            type: 1,
            area: ['420px', '440px'], //宽高
            btn: ['关闭'], //可以无限个按钮
            yes: function(index, layero){
                layer.close(roleAuthLayer);
            },
            btnAlign: 'c',
            content: html
        });

        var ztree = new ztreeUnit('treeDemo', '/groupManager/ajax/getGroupZtree', params);
        ztree.ztreeInit();
    },

};

groupManager_page.data = {
    'tableIns':undefined,
    'dtreeIns':undefined,
    'currGroupId':'',
    'action':""
};

AppComm.initFrame(groupManager_page);
